package it.polimi.swKnights.SWIMv2.ejb.administration;

import it.polimi.swKnights.SWIMv2.ejb.authentication.LoginHandlerLocal;
import it.polimi.swKnights.SWIMv2.ejb.entityBeans.Admin;
import it.polimi.swKnights.SWIMv2.ejb.entityBeans.BannedUser;
import it.polimi.swKnights.SWIMv2.ejb.entityBeans.RegisteredUser;
import it.polimi.swKnights.SWIMv2.ejb.exceptions.IllegalBanException;
import it.polimi.swKnights.SWIMv2.ejb.exceptions.InvalidLoginPrivilegesException;
import it.polimi.swKnights.SWIMv2.ejb.exceptions.LoginFailException;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 * Session Bean implementation class BanUser
 */
@Stateless
public class BanUser extends AdministrationBean implements BanUserRemote
{
	@EJB
	private LoginHandlerLocal loginController;

	@EJB
	private BannedUtilityLocal bannedUtility;

	@PersistenceContext(unitName = "swimv2-manager")
	private EntityManager manager;

	@Override
	public void banUser(String userToBan, int banDays, String admin,
			String password) throws LoginFailException,
			InvalidLoginPrivilegesException, IllegalBanException
	{
		this.loginController.checkLoginDataAndPrivileges(admin, password,
				Admin.class);
		this.handleOperation(userToBan, banDays);
	}

	@Override
	public List<String> getBannedUsers(String admin, String password)
	{
		List<String> bannedUserList = new ArrayList<String>();
		try
		{
			this.loginController.checkLoginDataAndPrivileges(admin, password,
					Admin.class);

			String getAllBanned = "select u.userName from BannedUser as u";
			Query bannedQuery = manager.createQuery(getAllBanned);
			List<?> queryList = bannedQuery.getResultList();

			for (Object item : queryList)
			{
				String userName = (String) item;
				bannedUserList.add(userName);
			}
		}
		catch (LoginFailException e)
		{
		}
		catch (InvalidLoginPrivilegesException e)
		{
		}

		return bannedUserList;
	}

	@Override
	public void endUserBan(String userName, String admin, String password)
			throws LoginFailException, InvalidLoginPrivilegesException
	{
		this.loginController.checkLoginDataAndPrivileges(admin, password,
				Admin.class);
		BannedUser user = this.manager.getReference(BannedUser.class, userName);
		bannedUtility.endBanUser(user);
	}

	private void handleOperation(String userToBan, int banDays)
			throws IllegalBanException
	{
		if (banDays <= 0)
			return;
		if (this.manager.find(Admin.class, userToBan) != null)
			throw new IllegalBanException(
					"Non si può bannare un utente amministratore!");
		RegisteredUser user = this.manager
				.find(RegisteredUser.class, userToBan);
		try
		{
			BannedUser userBanned = this.createUser(user, BannedUser.class);
			Calendar cal = Calendar.getInstance();
			cal.setTime(new Date());
			cal.add(Calendar.DATE, banDays);
			Date endOfBan = cal.getTime();
			userBanned.setEndOfBan(endOfBan);
			this.manager.remove(user);
			this.manager.persist(userBanned);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			System.err.println("Errore in ban User");
		}
	}
}
